package com.hailong.zyt.controller;


import com.hailong.zyt.beans.Homework;
import com.hailong.zyt.beans.HomeworkStudent;
import com.hailong.zyt.beans.Student;
import com.hailong.zyt.beans.Teacher;
import com.hailong.zyt.service.HomeworkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hailong
 * @since 2021-06-01
 */
@Slf4j
@Controller
public class HomeworkController {

    private HomeworkService homeworkService;

    /*----------------------------------------------------------------------------------------------------------------------*/
    //教师展示作业
    @GetMapping("/teacher/homework/{couId}")
    public String showHomework(@PathVariable("couId") Long couId, HttpSession session, Model model){
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        homeworkService.getHomeworkByTeaId(teacher.getTeaId(), couId, model);

        return "teacher/course/teaCourse :: homeworkList";
    }

    //添加作业
    @GetMapping("/teacher/addHomework/{couId}")
    public String toAddHomeworkPage(@PathVariable("couId") Long couId , HttpSession session, Model model) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        model.addAttribute("couId", couId);
        model.addAttribute("teaId", teacher.getTeaId());
        return "/teacher/homework/addHomework";
    }
    @PostMapping("/teacher/addHomework")
    public String addHomework(Homework newHomework, String start, String end) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        newHomework.setHwStartTime(LocalDateTime.parse(start.replaceAll("T", " ") + ":00", df));
        newHomework.setHwEndTime(LocalDateTime.parse(end.replaceAll("T", " ") + ":00", df));
        homeworkService.save(newHomework);
        return "redirect:/teacher/course";
    }

    /*----------------------------------------------------------------------------------------------------------------------*/

    //学生展示作业
    @GetMapping("/student/homework/{couId}")
    public String showStudentHomework(@PathVariable("couId") Long couId, HttpSession session, Model model){
        Student student = (Student) session.getAttribute("student");
        homeworkService.getHomeworkByStuId(student.getStuId(), couId, model);
        return "student/course/stuCourse :: StudentHomeworkList";
    }

    //做作业
    @GetMapping("/student/doHomework/{hwId}")
    public String toDoHomeworkPage(@PathVariable("hwId") Long hwId, Model model, HttpSession session){
        Student student = (Student) session.getAttribute("student");
        Homework homework = homeworkService.getById(hwId);
        model.addAttribute("homework", homework);
        return "/student/homework/doHomework";
    }
    /*----------------------------------------------------------------------------------------------------------------------*/
    @Autowired
    public void setHomeworkService(HomeworkService homeworkService) {
        this.homeworkService = homeworkService;
    }
}

